<?php

abstract class Jf_View_Block_Tree extends Jf_View_Block_Abstract {

    protected $_nodes = array();

    protected $_data = null;

    protected $_container = null;

    public function __call ($method, array $arguments = array()) {
        $block = 'Jf_View_Block_Tree_' . ucfirst ($method);
        if ( ! $this -> _enabledCache) {
            return $this -> renderBlock ($block, $method, $arguments);
        } else {
            return $this -> getCache ($block, $method, $arguments);
        }
    }

    public function getCache ($block, $method, $arguments) {
        $request = Jf_Controller_Dispatcher::getInstance () -> getRequest ();
        $requestPath = $request -> getRequestPath ();
        $key = md5 (get_class ($this) . $method . $requestPath);
        $data = Jf::cache () -> get ($key);
        if (false === $data) {
            $data = $this -> renderBlock ($block, $method, $arguments);
            Jf::cache () -> set ($key, $data);
        }
        return $data;
    }

    protected function renderBlock ($block, $method, $arguments) {
        $block_class = new $block ();
        $arguments = array_merge (array('container' => $this -> getContainer ()), $arguments);
        return call_user_func_array (array($block_class, $method), $arguments);
    }

    abstract protected function getContainer ();

}

 